<?php

namespace Drinkiit\BlogBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
use Drinkiit\BlogBundle\Form\AdminOperationsUserType;
use Drinkiit\BlogBundle\Form\AdminOperationsCommandeType;
use Drinkiit\BlogBundle\Handler\AdminOperationsUserHandler;
use Drinkiit\BlogBundle\Handler\AdminOperationsCommandeHandler;
use Drinkiit\BlogBundle\Entity\Operations;

class AdminOperationsController extends Controller
{
	public function em($objet) {
		return $this->getDoctrine()->getEntityManager()->getRepository($objet) ;
	}
	
	public function indexAction()
	{
		$operation = new Operations();
		$operation->setDate(new \DateTime("now")) ;
		
		$form1 = $this->createForm(new AdminOperationsUserType, $operation);
		$form2 = $this->createForm(new AdminOperationsCommandeType($this->getDoctrine()->getEntityManager()), $operation);
		
        $formHandler1 = new AdminOperationsUserHandler($form1, $this->get('request'), $this, $this->getDoctrine()->getEntityManager());
        $formHandler2 = new AdminOperationsCommandeHandler($form2, $this->get('request'), $this, $this->getDoctrine()->getEntityManager());
		
        if( $formHandler1->process() )
        {
			$this->get('logger')->info("[INFO] Formulaire bien validé, d'ou la redirection [admin_management_operations]") ;
            return $this->redirect( $this->generateUrl('admin_management_operations') );
        }
		
        if( $formHandler2->process() )
        {
			$this->get('logger')->info("[INFO] Formulaire bien validé, d'ou la redirection [admin_management_operations]") ;
            return $this->redirect( $this->generateUrl('admin_management_operations') );
        }
		
		$liste_operations = $this->em('DrinkiitBlogBundle:Operations')->findAll() ;
		$this->get('logger')->info("[INFO] Affichage des ".count($liste_operations)." opérations de tous les membres.") ;
		return $this->render('DrinkiitBlogBundle:Admin:Operations.html.twig', array(
			'form1' => $form1->createView(),
			'form2' => $form2->createView(),
			'titre' => 'Ajouter une opération',
			'liste' => $liste_operations,
		));
	}
}
